package com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeDevices.adpater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeDevices.bean.DevicesBean;
import com.example.anew.journey.mynewjourney.inteface.OnRecyclerViewItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/3/15
 */

public class DevicesListAdapter extends  RecyclerView.Adapter<DevicesListAdapter.ViewHolder> implements View.OnClickListener{
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    List<DevicesBean> mlistBean = new ArrayList<>();
    private Context mContext;
    public DevicesListAdapter(Context mcontxt) {
        mContext = mcontxt;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view  =  View.inflate(parent.getContext(), R.layout.devics_list_item,null);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    public void addDate(List<DevicesBean> mlist){
        mlistBean.addAll(mlist);
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Glide.with(mContext).load(RBConstants.SERVER_URL +"/Public/Uploads/"+mlistBean.get(position).url).into(holder.iv_devices_pictru);
        holder.tv_devices_name.setText(mlistBean.get(position).name);
       int type =  mlistBean.get(position).getType();
            String  binding = mlistBean.get(position).binding;
            if(binding == null){
                banding(holder);
            }else{
                unBanding(holder);
            }

        holder.tv_remove_banding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onItemFirstButtonClick(view,position);
                }
            }
        });
    }

    private void unBanding(ViewHolder holder) {
        holder.tv_remove_banding.setText("解除绑定");
        holder.tv_remove_banding.setTextColor(0xff999999);
        holder.tv_remove_banding.setBackgroundResource(R.drawable.devics_list_item_control_display_binding);
    }

    private void banding(ViewHolder holder) {
        holder.tv_remove_banding.setText("绑定");
        holder.tv_remove_banding.setTextColor(0xff2196F3);
        holder.tv_remove_banding.setBackgroundResource(R.drawable.devics_list_item_control_display_unbinding);
    }

    public void clearItem(){
        mlistBean.clear();
    }
    @Override
    public int getItemCount() {
        return mlistBean.size();
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null){
            mOnItemClickListener.onItemClick(view);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private  ImageView iv_devices_pictru;
        private  TextView tv_devices_name;
        private  TextView tv_remove_banding;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_devices_pictru = itemView.findViewById(R.id.devics_list_item_iv_devices_pictru);
            tv_devices_name = itemView.findViewById(R.id.devics_list_item_tv_devices_name);
            tv_remove_banding = itemView.findViewById(R.id.devics_list_item_tv_remove_banding);
        }
    }
}
